package com.sophialex.health.record.common;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

import com.sophialex.health.common.platform.SuperContext;
import com.sophialex.health.common.platform.pojo.Geography;
import com.sophialex.health.record.common.utils.ImageUtil;
import com.sophialex.health.system.frame.widgets.treeCombo.TreeComboInit;



public class GeographyTreeInit implements TreeComboInit {

	private List<Geography> list;
	private Object objData;

	public Object getObjData() {
		return objData;
	}

	public void setObjData(Object objData) {
		this.objData = objData;
	}

	public String getAllSelectText() {

		return "";
	}
    
	private List initData(){
		List list = SuperContext.dsGeographyTreeList;
		return list;
	}
	
	public Tree initTree(Tree tree) {
		list = initData();
		TreeItem root = new TreeItem(tree, SWT.NONE);
		if (list != null && list.size() > 0) {
			root.setText(list.get(0).getfTreemc().trim());
			root.setData(list.get(0));
			root.setImage(ImageUtil.geog);
			buildTree(list.get(0).getBrachs(), root);
		}
		setTreeItemExpanded(root);
		return tree;
	}

	/**
	 * 递归创建一棵树
	 * 
	 * @param root
	 */
	private void buildTree(List<Geography> brachslist, TreeItem root) {

		for (Geography obj : brachslist) {
			TreeItem item1 = new TreeItem(root, SWT.NONE);
			item1.setText(obj.getfTreemc());
			item1.setData(obj);
			item1.setImage(ImageUtil.geog);
			if (obj.getBrachs() != null && obj.getBrachs().size() > 0) {
				buildTree(obj.getBrachs(), item1);
			}
		}
	}

	public List<Geography> getList() {
		return list;
	}

	public void setList(List<Geography> list) {
		this.list = list;
	}

	// 设置树节点展开模式
	private void setTreeItemExpanded(TreeItem it) {
		it.setExpanded(true);
		for (TreeItem item : it.getItems()) {
			item.setExpanded(true);
			if (item.getItemCount() != 0) {
				setTreeItemExpanded(item);
			}
		}
	}
}
